<?php

namespace App\Models\Admin;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;

class AdminUser extends Authenticatable{
    use EntrustUserTrait;

    //Big block of caching functionality.
    public function cachedRoles()
    {
        $userPrimaryKey = $this->primaryKey;
        $cacheKey = 'entrust_roles_for_user_'.$this->$userPrimaryKey;
        return Cache::store('array')->tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
            return $this->roles()->get();
        });
    }
    public function save(array $options = [])
    {   //both inserts and updates
        $result = parent::save($options);
        Cache::store('array')->tags(Config::get('entrust.role_user_table'))->flush();
        return $result;
    }
    public function delete(array $options = [])
    {   //soft or hard
        $result = parent::delete($options);
        Cache::store('array')->tags(Config::get('entrust.role_user_table'))->flush();
        return $result;
    }
    public function restore()
    {   //soft delete undo's
        $result = parent::restore();
        Cache::store('array')->tags(Config::get('entrust.role_user_table'))->flush();
        return $result;
    }

    //设置表的类型和表名
    protected $table='admin_users';
    protected $primaryKey='id';
    protected $fillable = [
        'name', 'email', 'password','is_super','remember_token'
    ];
    //用户和角色之间多对多关系
    public function roles(){
        return $this->belongsToMany('App\Models\Admin\Role','role_user','user_id','role_id');
    }


}